{% extends 'layouts/default/page.html' %}

{% load bootstrap4 %}
{% load i18n %}

{% block content %}

{% if user.is_authenticated %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Video List</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <h1>Available Videos</h1>
    <ul>
        {% for video in videos %}
        <li>
            {{ video.name }}
            <button class="view-button" data-id="{{ video.id }}">查看</button>
            <div id="video-{{ video.id }}" style="display: none;">
                <!-- 这里将动态显示视频 -->
            </div>
        </li>
        {% endfor %}
    </ul>

    <script>
        // 监听查看按钮的点击事件
        $('.view-button').on('click', function() {
            var videoId = $(this).data('id');
            var videoContainer = $('#video-' + videoId);
            console.log(videoId)

            // 发起 AJAX 请求，获取视频路径
            $.ajax({
                url: '/videos/' + videoId + '/',
                method: 'GET',
                success: function(data) {
                    var videoHtml = '<video controls width="320"><source src=' + data.path + ' type="video/mp4">Your browser does not support the video tag.</video>';
                    videoContainer.html(videoHtml).show();
                    console.log(videoHtml);
                },
                error: function() {
                    alert('Error loading video');
                }
            });
        });
    </script>
</body>
</html>
{% else %}
<div class="alert alert-warning">
    您还未登录。
</div>
{% endif %}

{% endblock %}
